package com.xsz.service;

import com.xsz.entity.Position;
import com.xsz.entity.User;
import com.xsz.repository.PositionRepository;
import com.xsz.util.KeyUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class PositionService {
    @Resource
    PositionRepository positionRepository;

//    /**根据行业类别查询所有职位**/
//    public List<String> showByTid(String tid){
//        return positionRepository.showByTid(tid);
//    }
//
//    /**根据行业类别查询所有职位**/
//    public List<Position> showByNameAndTid(String name, String tid){
//        return positionRepository.findByNameAndPid(name, tid);
//    }

    /**添加职位**/
    public Position addPosition(Position position, User user) {

        position.setCreateTime(new Date());
        position.setId(KeyUtil.getId());
        position.setCreateBy(user.getId());

        return positionRepository.save(position);
    }

    /**修改职位**/
    public Position editPosition(Position position) {
        position.setCreateTime(new Date());
        return positionRepository.save(position);
    }

    /**删除职位**/
    public void deletePosition(String id) {
        positionRepository.deleteById(id);
    }

    /**查询所有职位**/
    public List<Position> showAll() {
        return positionRepository.findAll();
    }

    /**根据id查询职位**/
    public Position showById(String id) {
        return positionRepository.findById(id).get();
    }

    public List<Position> showAllByCreateById(String createById) {
        System.out.println("showAllByCreateById***"+createById);
        return positionRepository.findByCreateBy(createById);
    }

    /**分页返回所有职位**/
    public Page<Position> showAll2(String page, String limit){
        Pageable pageable = PageRequest.of(Integer.parseInt(page), Integer.parseInt(limit));
        return positionRepository.findAll(pageable);
    }

    /**返回搜索职位**/
    public List<Position> showSearch(String search){
        return positionRepository.findByNameLike("%"+search+"%");
    }
}